<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>服务通知列表</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<link rel="stylesheet" href="../../../css/mui.min.css">
		<link rel="stylesheet" href="../../../css/global.css">
		<link rel="stylesheet" href="../../../css/style.css">
		<link rel="stylesheet" href="../../../css/checkbox.css"/>
		<style>
			.mui-checkbox input[type=checkbox], .mui-radio input[type=radio]{
				top: 8px;
				right: -10px;
			}
			.item_box .item .name{
			    height: auto;
				line-height: initial;
				padding: 5px 17px;
			}
		</style>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
			<h1 id="title" class="mui-title">服务通知列表</h1>
		</header>
		<div style="height: 30px;"></div>
		<div class="item_box">
			<div class="details_con" id="liebiao">
			</div>
			<div class="btn_box">
				
				<span class="fl" id="quxiao">取消</span>
				<span class="fr red" id="save">确定</span>
			</div>
			<div style="height: 50px;"></div>
		</div>

		<!-- js代码 start -->

		<!-- 基础js start -->
		<script src="../../../js/mui.min.js"></script>
		<script src="../../../js/utils/h.js"></script>
		<script src="../../../js/constant.js"></script>
		<script src="../../../js/common.js"></script>
		<script src="../../../js/utils/base64.js"></script>
		<script src="../../../js/utils/md5.js"></script>
		<script src="../../../js/utils/sortMap.js"></script>
		<script src="../../../js/utils/http.js"></script>
		<script src="../../../js/app.js"></script>
		<script src="../../../js/sqliteTools.js"></script>
		<!-- 基础js end -->

		
		<script type="text/javascript" charset="utf-8">
			 //mui初始化
			var ServiceNoticeId;
			var serviceNo;
			var planId;
			var state;
			var stateName;
			var longitude;
			var latitude;
			var ServiceNoticeIds = "";
			var serviceNos = "";
			var planIds = "";
			var customerName;
			(function($, doc) {
				$.init();
				
				$.plusReady(function() {
					
					var self = plus.webview.currentWebview();
					 // 获取上一个页面传过来的参数并显示到页面
					 ServiceNoticeId = self.ServiceNoticeId;
					 serviceNo = self.serviceNo;
					 planId = self.planId;
					 state = self.state;
					 
					 // 获取定位的经纬度 
					 app.showWaiting('加载中...');
					 plus.geolocation.getCurrentPosition(function(p){
					 	longitude = p.coords.longitude;
					 	latitude = p.coords.latitude;
						
						// 获取服务通知类别
						getFWList();
					 }, function(e){
						app.closeWaiting();
					 	app.toast("定位失败");
					 }, {geocode: false});
					 					 
					 var signIn = function() {
						 // 获取checkbox的值
						 var ele = document.getElementsByName("checkbox1");
						  ServiceNoticeIds = "";
						  serviceNos = "";
						  planIds = "";
						 for (var i = 0; i < ele.length; i++) {
							if(ele[i].checked){
								planIds += ele[i].value+",";
								ServiceNoticeIds += document.getElementById(ele[i].value).getAttribute('serviceNoticeId') + ",";
								serviceNos += document.getElementById(ele[i].value).getAttribute('serviceNo') + ",";
							}
						 }
						 // 加上之前页面传过来的参数
						 // planIds += planId;
						 // ServiceNoticeIds += ServiceNoticeId;
						 // serviceNos += serviceNo;
						 planIds = planIds.slice(0, planIds.length-1);
						 ServiceNoticeIds = ServiceNoticeIds.slice(0, ServiceNoticeIds.length-1);
						 serviceNos = serviceNos.slice(0, serviceNos.length-1);
						//判断地址是否有变更
						ajaxGet(BASE_URL + "cc/instrument/checkAddressDistance", {
							"ServiceNoticeId": ServiceNoticeId,
							"longitude": longitude,
							"latitude": latitude
						}, function(data) {
							app.closeWaiting()
							if(data.code==200){
								if(data.data){ //跳转地址变更页
									// app.jumpTo("/view/service/addressChange/addressChange.html", "view/service/addressChange/addressChange.html",
									// {
									// 	serviceNos: serviceNos,
									// 	ServiceNoticeIds: ServiceNoticeIds,
									// 	noticeId: ServiceNoticeId
									// });
									var addressChange = plus.webview.create('/view/service/addressChange/addressChange.html','view/service/addressChange/addressChange.html',null,
									{
										serviceNo: serviceNo,
										serviceNos: serviceNos,
										ServiceNoticeIds: ServiceNoticeIds,
										planIds: planIds,
										noticeId: ServiceNoticeId,
										customerName: customerName
									})
									addressChange.show()
								}else{ 
									qiandao6()
								}
							}else{
								 app.toast(data.msg)
							}
						}, function(errorMsg) {
							 app.toast(errorMsg)
							 app.closeWaiting()
						});
						
					 }
					 
					 h("#quxiao").tap(function(){
						 mui.back()
					 })
					 
					 // 确定按钮方法
					 h("#save").tap(function(){
						 // 获取checkbox的值
						 var ele = document.getElementsByName("checkbox1");
						  ServiceNoticeIds = "";
						  serviceNos = "";
						  planIds = "";
						 for (var i = 0; i < ele.length; i++) {
							if(ele[i].checked){
								planIds += ele[i].value+",";
								ServiceNoticeIds += document.getElementById(ele[i].value).getAttribute('serviceNoticeId') + ",";
								serviceNos += document.getElementById(ele[i].value).getAttribute('serviceNo') + ",";
							}
						 }
						 // 加上之前页面传过来的参数
						 // planIds += planId;
						 // ServiceNoticeIds += ServiceNoticeId;
						 // serviceNos += serviceNo;
						 // console.log("planIds:"+planIds+"	ServiceNoticeIds:"+ServiceNoticeIds+"	serviceNos:"+serviceNos)
						 planIds = planIds.slice(0, planIds.length-1);
						 ServiceNoticeIds = ServiceNoticeIds.slice(0, ServiceNoticeIds.length-1);
						 serviceNos = serviceNos.slice(0, serviceNos.length-1);
						 if(serviceNos == ""){
						 	app.toast("请勾选服务通知");
						 	return
						 }
			
						 signIn()
					 });
				});
			}(mui, document));
			
			// 获取服务通知类别
			function getFWList(){
				ajaxGet(BASE_URL + "cc/notice/AssociatedCustomers", {
					"ServiceNoticeId": ServiceNoticeId,
					"EnginnerId": app.getUser().enginnerId,
					"state": state,
					// "planId": planId
				}, function(data) {
					app.closeWaiting();
					var html = "";
					h("#liebiao").html("");
					if(data.data && data.data.length > 0){
						customerName = data.data[0].customerName;
						for(var i=0;i<data.data.length;i++){
							var dts = data.data[i];
							var hopeBusinessHours = "";
							var isChecked = "";
							if (dts.hopeBusinessHours != undefined && dts.hopeBusinessHours != 'undefined' && dts.hopeBusinessHours !="")
							{
							    hopeBusinessHours = dts.hopeBusinessHours;
							}
							if (dts.serviceNo == serviceNo){
								isChecked = 'checked';
							}
							
							html += '<div class="item">'
								html += '<div class="bar">'
									html += '<span class="number">'+dts.serviceNo+'<img src="../../../images/icon_204.png" /></span>'
									html += '<div class="checkbox">'
										html += '<div class="mui-input-row mui-checkbox" id="'+dts.id+'" serviceNoticeId='+dts.serviceNoticeId+' serviceNo='+dts.serviceNo+' >'
											html += '<input name="checkbox1" value="'+dts.id+'" type="checkbox" class="rdss" '+ isChecked +'/>'
											html += '<label></label>'
										html += '</div>' 
									html += '</div>'
								html += '</div>'
								html += '<div class="name">'
									html += dts.customerName
								html += '</div>'
								html += '<div class="type">'
									html += '<p>提交时间：<span>'+dts.createTime+'</span><img src="../../../images/icon_202.png"/></p>'
									//默认0待接受，1已接受，2已签到，3已签离，4已完成,5自动签离，6已拒绝
									state = dts.state;
									if(state == '0'){
										stateName = "待接受";
									}else if(state == '1'){
										stateName = "已接受";
									}else if(state == '2'){
										stateName = "已签到";
									}else if(state == '3'){
										stateName = "已签离";
									}else if(state == '4'){
										stateName = "已完成";
									}else if(state == '5'){
										stateName = "自动签离";
									}else if(state == '6'){
										stateName = "已拒绝";
									}
									html += '<p>状态：<span>'+stateName+'</span><img src="../../../images/icon_205.png"/></p>'
									html += '<p>服务子类型：<span>' + dts.subServiceType + '<span><img src="../../../images/icon_203.png"/></p>'
									html += '<p>期望服务时间：<span>' + hopeBusinessHours + '</span><img src="../../../images/icon_202.png"/></p>'
								html += '</div>'
							html += '</div>';
						}
						h("#liebiao").html(html);
					}
				}, function(errorMsg) {
					app.closeWaiting();
					app.toast(errorMsg);
				});
			}
			
			function qiandao6(){
				ajaxGet(BASE_URL + "cc/PlanDetails/fseSign", {
					"relServiceNo": serviceNo,
					"ServiceNoticeIds": ServiceNoticeIds,
					"serviceNos": serviceNos,
					"planIds": planIds,
					"EnginnerId": app.getUser().enginnerId,
					"longitude": longitude,
					"latitude": latitude
				}, function(data) {
					app.closeWaiting()
					if(data.code==200){ 
						if(data.data && data.data.length > 0){
							// 判断是否存在离线数据，
							var enginnerId = app.getUser().enginnerId;
							var fseName = app.getUser().name;
							var sqliteTool = new sqliteTools(); 
							sqliteTool.openDatabase();
							for (var i = 0; i < data.data.length; i++) {
								var resData = data.data[i];	
								sqliteTool.selectServiceByPlanId(enginnerId, resData.planId, resData, function(flag, offlineData, callBackData) {
									if(flag){	
										if (offlineData && offlineData.length > 0) {
											sqliteTool.insertServiceSignInData(offlineData[0].id, "1", callBackData.signInTime, longitude, latitude, "0", function(flag) {
												console.log("更新本地签到数据"+flag)										
											})
										} else {
											sqliteTool.insertServiceBaseAndSignInData(callBackData.serviceNo, callBackData.serviceNoticeId, callBackData.planId, customerName, callBackData.instrumentId, callBackData.instSn, callBackData.acceptTime, enginnerId, fseName, callBackData.serviceType, callBackData.subServiceType, "1", callBackData.signInTime, longitude, latitude, function (flag) {
												console.log("插入本地签到数据"+flag)											
											})									
										}
									}
								});
							}	
							setTimeout(function() {
								sqliteTool.closeDB();
								app.toast("签到成功")
								tiaozhuan()
							}, 500);													
						} else {
							app.toast("签到成功")
							tiaozhuan()
						}
					}else{
						app.toast(data.msg)
						app.closeWaiting()
					}
				}, function(errorMsg) {
					app.toast(errorMsg)
					app.closeWaiting()
				});
			}
			
			function tiaozhuan(){
				var signIn = plus.webview.getWebviewById('view/service/notice/signIn.html');
				if (signIn) {
					signIn.close()
				}
				
				var signInAddReport = plus.webview.getWebviewById('view/service/notice/signInAddReport.html');
				if (signInAddReport) {
					signInAddReport.close()
				}
				
				// 关闭进行中
				var conductList = plus.webview.getWebviewById('view/service/assign/conductList.html');
				if(conductList){
					conductList.close()
				}
				
				// 关闭工作台已签离列表
				var checkedOutList = plus.webview.getWebviewById('view/service/notice/checkedOutList.html');
				if(checkedOutList){
					checkedOutList.close()
				}
				
				// 关闭工作台查询未关闭服务通知列表
				var notClosedList = plus.webview.getWebviewById('view/service/notice/notClosedList.html');
				if(notClosedList){
					notClosedList.close()
				}
				
				// 1、刷新FSE首页
				var indexFSE = plus.webview.getWebviewById('view/main/indexFSE.html');
				if(indexFSE){
					indexFSE.evalJS('getList("1")')
				}
				
				// 关闭工作台已接受列表
				var acceptedList = plus.webview.getWebviewById('view/service/notice/acceptedList.html');
				if(acceptedList){
					acceptedList.close()
				}
				
				var self = plus.webview.currentWebview();
				var checkedInList = plus.webview.getWebviewById('view/service/notice/checkedInList.html');
				if(checkedInList){
					checkedInList.evalJS('initData()')
					mui.back()
				}else{
					var checkedInList1 = plus.webview.create('/view/service/notice/checkedInList.html','view/service/notice/checkedInList.html')
					checkedInList1.show()
					self.close();
				}
				
			}
		</script>
		<!-- js代码 end -->

	</body>

</html>
